﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class Territory : IEquatable<Territory>
    {
        public void ImportList(List<Classes.Territory> terList)
        {
            var mdb = new MedregDBDataContext();
            var ListTer = new List<Territory>();

            var sKLADR = new Classes.KLADR();
            var KLADR = new KLADR();
            

            foreach (var ter in terList)
            {
                Territory ter1 = new Territory()
                                     {
                                         ID = ter.ID, 
                                         Name = ter.Name, 
                                         Parent = ter.Parent, 
                                         Order = ter.Order, 
                                         OUZ = ter.OUZ,
                                         KLADR = ter.KLADR
                                     };
                Territory query;

                if (mdb.Territory.LongCount() == 0)
                {
                    mdb.Territory.InsertOnSubmit(ter1);
                }
                else
                {
                    try
                    {
                        query = mdb.Territory.Single(l => l.ID == ter1.ID);
                        query.Name = ter1.Name;

                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {

                        if (!ListTer.Contains(ter1))
                        {
                            mdb.Territory.InsertOnSubmit(ter1);
                            ListTer.Add(ter1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(Territory other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
